package com.ray.auth.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ray.auth.system.service.AppService;
import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.table.entity.App;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.common.util.DataGridUtil;
import com.ray.common.util.DictLoadUtil;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.web.controller.WebController;


@Controller
@RequestMapping("/system/app")
public class AppController extends WebController{

	@Autowired
	private ButtonService buttonService;
	@Autowired
	private AppService appService;
	@Autowired
	private DictService dictService;
	@Autowired 
	private DictLoadUtil DictLoadUtil;
	
	@RequestMapping(value="view")
	@RequiresPermissions("app:view")
	public ModelAndView view(String menuId, HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		  LoginUser user = getLoginUser();
		  Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		  modelAndView.addObject("buttons",buttons);
		  modelAndView.setViewName("system/app/view");
		  return modelAndView;
		
	}
	/**
	 * 新增页面
	 * @param menuId
	 * @param modelAndView
	 * @return
	 */
	@RequestMapping(value="appAddView")
	@RequiresPermissions("app:add")
	public ModelAndView addView(String menuId,App params,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
		Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","appType");
	
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("buttons",buttons);
		modelAndView.addObject("record",params);
		modelAndView.setViewName("system/app/add");
		return modelAndView;
	}
	
	/**
	 * 应用新增
	 */
	@RequestMapping(value = "appAdd")
	@RequiresPermissions("app:add")
	@ResponseBody
	public ResponseEntity<Result<Object>> add(App params,HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		Result<Object> result= appService.addApp(params,user);
		return doResult(result);
	}
	/*
	 * 应用修改页面
	 */
	@RequestMapping(value = "appUpdateView")
	@RequiresPermissions("app:edit")
	@ResponseBody
	public ModelAndView updateView(String menuId,String id,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user= getLoginUser();
		Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","appType");
		App params=appService.queryApp(id,user);
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("buttons", buttons);
		modelAndView.addObject("record",params );
		modelAndView.setViewName("system/app/update");
		return modelAndView;
		   
	}
	
	/**
	 * 应用修改
	 */
	@RequestMapping(value = "appUpdate")
	@RequiresPermissions("app:edit")
	@ResponseBody
	public ResponseEntity<Result<Object>> update(App params,HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		Result<Object> result=appService.updateApp(params,user);
		return doResult(result);
		
	}
	
	/**
	 * 应用详情页面
	 */
	@RequestMapping(value = "appDetailView")
	@RequiresPermissions("app:detail")
	@ResponseBody
	public ModelAndView detailView(String menuId,String id,HttpServletRequest request, HttpServletResponse respose,
			ModelAndView modelAndView){
		LoginUser user = getLoginUser();
		Map<String,List<Button>> buttons=buttonService.queryUserButtonsWithMenu(user, menuId);
		App params = appService.queryApp(id,user);
		Map<String,List<Dict>> dicts = DictLoadUtil.loadDict(dictService,"useStatus","appType");
		modelAndView.addObject("dicts", dicts);
		modelAndView.addObject("buttons", buttons);
		modelAndView.addObject("record",params );
		modelAndView.setViewName("system/app/detail");
		return modelAndView;
		   
	}
	/**
	 * 应用删除页面
	 */
	@RequestMapping(value = "appDelete")
	@RequiresPermissions("app:delete")
	@ResponseBody
	public ResponseEntity<Result<Object>> delete(String id, HttpServletRequest request, HttpServletResponse respose){
		LoginUser user =getLoginUser();
		Result<Object> result=appService.deleteApp(id,user);
		return doSuccess(result);
		
	}
	/**
	 * 应用查询页面
	 */
	@RequestMapping(value = "query")
	@RequiresPermissions("app:query")
	@ResponseBody
    public ResponseEntity<Map<String,Object>> query(App params, HttpServletRequest request, HttpServletResponse respose){
		LoginUser user=getLoginUser();
		PageDto<App> page= this.createJQPage(request);
		page= appService.queryApp(params,page,user);
		return doSuccess(DataGridUtil.dataConvert(page));

	}	
}
